package com.longmao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longmao.commonutils.R;
import com.longmao.eduservice.entity.EduCourse;
import com.longmao.eduservice.entity.vo.CourseInfoVo;
import com.longmao.eduservice.entity.vo.CoursePublishVo;
import com.longmao.eduservice.entity.vo.CourseQuery;
import com.longmao.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author longmao
 * @since 2022-01-27
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 添加课程基本信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", courseId);
    }

    @PostMapping("/getCourseList/{page}/{limit}")
    public R getCourseList(@PathVariable Long page, @PathVariable Long limit, @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 根据id进行查询
     */
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfo) {
        courseService.updateCourseInfo(courseInfo);
        return R.ok();
    }

    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo publishCourseInfo = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("coursePublishVo", publishCourseInfo);
    }

    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        boolean result = courseService.publishCourseById(courseId);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("发布失败");
        }
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        boolean result = courseService.deleteCourseById(courseId);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }
}

